﻿using System.Collections.Generic;
using Firefly.Framework.Windows.ContentReaders;
using Firefly.Framework.Windows.Forms;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content.Pipeline.Serialization.Compiler;

namespace Firefly.ContentPipeline.Windows.Forms
{
    [ContentTypeWriter]
    public class ControlContentWriter : ContentTypeWriter<Control>
    {
        protected override void Write(ContentWriter output, Control value)
        {
            WriteComponent(output, value);
        }

        public override string GetRuntimeReader(TargetPlatform targetPlatform)
        {
            return typeof(ControlContentReader).AssemblyQualifiedName;
        }

        internal static void WriteComponent(ContentWriter output, Control value)
        {
            output.Write(value.Name);
            output.WriteObject<Rectangle>(value.Position);
            output.WriteObject<Vector4>(value.Padding);
            output.Write(value.Color);
            output.Write(value.Cursor);
            output.Write(value.Text);
            output.WriteObject<Vector4>(value.TextOffset);
            output.WriteObject<Vector4>(value.ClientAreaOffset);
            output.Write(value.Font);
            output.Write(value.FontColor);
            output.Write((int)value.DockStyle);
            output.Write(value.InheritColor);
            output.Write(value.InheritFont);
            output.Write(value.InheritFontColor);
            output.Write(value.TabNext);
            output.Write(value.TabPrev);
            output.Write(value.Visible);
            output.Write(value.DrawOrder);
            output.Write(value.Enabled);
            output.Write(value.UpdateOrder);
            // This next line only writes children that want to be written. This is used because
            // some components like Memo create their own child components at runtime, and these
            // self creating components are not part of the content.
            output.WriteObject<List<Control>>(value.Children.FindAll(delegate(Control c) { return c.WriteToContent; }));
        }
    }
}
